<?php
/**
 * Created by PhpStorm.
 * User: dreamy
 * Date: 2022-04-25
 * Time: 12:14
 */
namespace common\services;

use common\models\DomainAuthCode;
use Yii;
use yii\helpers\ArrayHelper;

/**
 * Class DomainAuthService
 *
 * @package common\services
 */
class DomainAuthService extends  BaseService
{

    /**
     * 检查授权码是否可用
     * @param $alias
     * @param $code
     *
     * @return bool
     */
    public static function checkValid($alias,$code)
    {
        $info = DomainAuthCode::find()->where([
            'alias' => $alias,
            'code' => $code,
            'state' => DomainAuthCode::STATE_UNUSED
        ])->one();
        return $info ? true : false;
    }

    /**
     * 标记某个授权码已被使用
     * @param     $alias
     * @param     $code
     * @param int $uid
     *
     * @return bool
     */
    public static function setUsed($alias,$code,$uid=0)
    {
        $info = DomainAuthCode::find()->where([
            'alias' => $alias,
            'code' => $code,
        ])->one();
        if(!$info) {
            self::setError('授权码记录不存在');
            return false;
        }
        $info->state = DomainAuthCode::STATE_USED;
        $info->uid = $uid;
        $res = $info->save();
        if($res) {
            return true;
        }else {
            self::setError('更新失败');
            return false;
        }
    }

}
